import { defineStore } from 'pinia'

export const orderStore = defineStore('orderStore',{
	state: ()=>{
		return {
			count:0,
			//购物车数据
			orderList:[]
		}
	},
	actions:{
		//单条数据加入
		addOrder(goods){
			//判断提交的商品是否已经存在于购物车
			const flag = this.orderList.findIndex(item => goods.goodsId == item.goodsId)
			if(flag > -1){//存在,删除原来的数据
				this.orderList.splice(flag,1)
			}
			this.orderList.push(goods)
		},
		//多条数据加入
		addOrderList(list){
			list.forEach(goods=>{
				//判断提交的商品是否已经存在于购物车
				const flag = this.orderList.findIndex(item => goods.goodsId == item.goodsId)
				if(flag > -1){//存在,删除原来的数据
					this.orderList.splice(flag,1)
				}
				this.orderList.push(goods)
			})
		},
	},
})